package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;

import com.happyscan138.makefun.R;
import com.happyscan138.makefun.base.VideoInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/30.
 */

public class BattleAdapter extends BaseAdapter{
    private MediaController mMediaCtrl;
    private int currentIndex=-1;
    private int playPosition=-1;
    private boolean isPaused=false;
    private boolean isPlaying=false;
    private VideoView mVideoView;
    public BattleAdapter(Context mContext, List<VideoInfo> videoList) {
        this.mContext = mContext;
        this.videoList = videoList;
    }

    private Context mContext;
    private List<VideoInfo>  videoList=new ArrayList<VideoInfo>() ;

    @Override
    public int getCount() {
        return videoList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder_BattleGood holder;
        final int mPosition=position;
        if(convertView==null){
            convertView=View.inflate( mContext, R.layout.battle_new_listview_item, null);
            holder=new ViewHolder_BattleGood();
      //      holder.videoImage=(ImageView) convertView.findViewById(R.id.battle_new_video_image);
      //      holder.videoPlayBtn=(ImageButton)convertView.findViewById(R.id.battle_new_video_play_btn);
        //    holder.mProgressBar=(ProgressBar) convertView.findViewById(R.id.battle_new_progressbar);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder_BattleGood) convertView.getTag();
        }
//        holder.videoImage.setImageDrawable(mContext.getResources().getDrawable(videoList.get(position).getVideoImage()));
        holder.videoPlayBtn.setVisibility(View.VISIBLE);
        holder.videoImage.setVisibility(View.VISIBLE);
        mMediaCtrl = new MediaController(mContext,false);
        if(currentIndex == position){
            holder.videoPlayBtn.setVisibility(View.INVISIBLE);
            holder.videoImage.setVisibility(View.INVISIBLE);

            if(isPlaying || playPosition==-1){
                if(mVideoView!=null){
                    mVideoView.setVisibility(View.GONE);
                    mVideoView.stopPlayback();
                    holder.mProgressBar.setVisibility(View.GONE);
                }
            }
            mVideoView=(VideoView) convertView.findViewById(R.id.battle_new_videoview);
            mVideoView.setVisibility(View.VISIBLE);
            mMediaCtrl.setAnchorView(mVideoView);
            mMediaCtrl.setMediaPlayer(mVideoView);
            mVideoView.setMediaController(mMediaCtrl);
            mVideoView.requestFocus();
            holder.mProgressBar.setVisibility(View.VISIBLE);
            if(playPosition>0 && isPaused){
                mVideoView.start();
                isPaused=false;
                isPlaying=true;
                holder.mProgressBar.setVisibility(View.GONE);
            }else{
    //            mVideoView.setVideoPath(videoList.get(mPosition).getUrl());
                isPaused=false;
                isPlaying=true;
                System.out.println("播放新的视频");
            }
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if(mVideoView!=null){
                        mVideoView.seekTo(0);
                        mVideoView.stopPlayback();
                        currentIndex=-1;
                        isPaused=false;
                        isPlaying=false;
                        holder.mProgressBar.setVisibility(View.GONE);
                        notifyDataSetChanged();
                    }
                }
            });
            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    holder.mProgressBar.setVisibility(View.GONE);
                    mVideoView.start();
                }
            });

        }else{
            holder.videoPlayBtn.setVisibility(View.VISIBLE);
            holder.videoImage.setVisibility(View.VISIBLE);
            holder.mProgressBar.setVisibility(View.GONE);
        }

        holder.videoPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex=mPosition;
                playPosition=-1;
                notifyDataSetChanged();
            }
        });
        return convertView;
    };
}
class ViewHolder_BattleGood{
    ImageView videoImage;
    ImageButton videoPlayBtn;
    ProgressBar mProgressBar;
}